package be.fomp.jeve.application;

import be.fomp.jeve.application.caching.Cacher;
import be.fomp.jeve.application.caching.DefaultCacher;
import be.fomp.jeve.application.caching.DefaultCacherDisabled;
import be.fomp.jeve.application.exceptions.JEveCacheLoadingException;
import be.fomp.jeve.application.exceptions.JEveCacheStoringException;

public abstract class AbstractJeveObjectFactory  implements Cacher{
	private final Cacher cacher;
	
	protected static final String CONNECTION_EXCEPTION = "File did not exist in cache and loading from web failed:";
	protected static final String PARSING_EXCEPTION = "File did not exist in cache and parsing date failed:";
	
	/**
	 * Main constructor.<br />
	 * When invoked the caching will be set to default which is the user home folder in the filesystem.<br />
	 * If you want to disable caching, set the parameter as null.
	 */
	public AbstractJeveObjectFactory() {
		this.cacher = new DefaultCacher();
		
	}
	
	/**
	 * Constructor to override the default caching.<br />
	 * If null, caching will be disabled.<br />
	 * @param cacher
	 */
	public AbstractJeveObjectFactory(Cacher cacher) {
		if(cacher == null) //Null means caching is disabled
			this.cacher = new DefaultCacherDisabled();
		else
			this.cacher = cacher;
	}

	public Object load(String location) throws JEveCacheLoadingException {
		return this.cacher.load(location);

	}

	public void store(Object jeveObject, String location) throws JEveCacheStoringException {
		this.cacher.store(jeveObject, location);

	}
}
